import SystemDataSys from "./SystemDataSys";

class FormData {
    public _boundary_key = "AaB03x";
    public _boundary = "";
    public _end_boundary = "";
    public _result = "";
    public _formResult = "";
    constructor() {
        this._boundary_key = "AaB03x";
        this._boundary = "";
        this._end_boundary = "";
        this._result = "";
        this._formResult = "";
        this._boundary = "--" + this._boundary_key;
        this._end_boundary = this._boundary + "--";
        this._result = "";
    }

    append(e, t) {
        this._result += this._boundary + "\r\n";
        this._result += 'Content-Disposition: form-data; name="' + e + '"\r\n\r\n';
        this._result += t + "\r\n";
    }

    arrayBuffer() {
        this._formResult = this._result + this._end_boundary;
        var e = [];
        if (!SystemDataSys.encrypt) {
            this._formResult = this.ch2Unicdoe(this._formResult);
        }
        for (var t = 0; t < this._formResult.length; t++) {
            e.push(this._formResult.charCodeAt(t));
        }
        return new Uint8Array(e).buffer;
    }

    ch2Unicdoe(e) {
        if (!e) return "";
        var t = "";
        var a = new RegExp("[一-龥]+");
        var o = new RegExp("[`~！@#￥……&（）——|，、？]");
        for (var i = 0; i < e.length; i++) {
            var r = e.charAt(i);
            if (a.test(r)) {
                t += "\\u" + r.charCodeAt(0).toString(16);
            } else if (o.test(r)) {
                t += "\\u" + r.charCodeAt(0).toString(16);
            } else {
                t += r;
            }
        }
        return t;
    }
}

const formData = new FormData();
export default formData;